<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Transaksi extends CI_Controller {

    function __construct() {
        parent::__construct();

        /* Standard Libraries of codeigniter are required */
        $this->load->database();
        $this->load->helper('url');
        /* ------------------ */

        $this->load->library('ExCRUD');
    }

    function request_pendaftaran() {
        $crud = new ExCRUD();
        $crud->standartCheck();

        // 1. set table & relation
        $crud->set_table('request_daftar');
        $crud->set_primary_key("id_request");
        $crud->set_relation('id_member', 'member', '{alias_member} ({nickname})');
        $crud->where('is_approved', '0');
        $crud->order_by('id_request', 'asc');

        // 2. set table display
        $crud->set_delete(false);

        $list_column_views = array('id_member', 'nama_registran', 'no_hp_registran', 'email_registran', 'date_submit');
        $crud->columns($list_column_views);

        $crud->display_as("id_member", "Sponsor");
        $crud->display_as("alias_member", "Sponsor");
        $crud->display_as("date_submarray('AKTIF')it", "Tanggal Daftar");
        $crud->display_as("is_approved", "<b>Pilih Respon</b>");

        // 3. aturan kolom
        $list_column_ro = array();
        $list_hidden_field = array();
        $required_fields = array();
        if ($crud->getState() == 'add') {
            $list_hidden_field = array('date_activated', 'id_request_pin', 'date_submit', 'is_approved', 'alias_member');
            $required_fields = array('upline', 'nama_registran', 'no_hp_registran');
        } else if ($crud->getState() == 'edit') {
            $crud->display_as("id_member", "ID Sponsor");
            $list_hidden_field = array('id_member', 'date_activated');
            $list_column_ro = array('date_submit', 'alias_member');
            $required_fields = array('nama_registran', 'no_hp_registran', 'is_approved');
            $crud->field_type("id_request_pin", "hidden", "-7");
        } else {
            $list_hidden_field = array('id_request_pin', 'is_approved', 'date_activated', 'alias_member');
        }
        $crud->field_type('is_approved', 'dropdown', array('1' => 'TERIMA PERMINTAAN', '-1' => 'TOLAK PERMINTAAN'));
        $crud->set_field_readonly($crud, $list_column_ro);
        $crud->set_field_hidden($crud, $list_hidden_field);
        $crud->required_fields($required_fields);

        // callback action
        $crud->callback_before_update(array($this, 'request_pendaftaran_after_update'));
        $crud->callback_after_insert(array($this, 'request_pendaftaran_after_insert'));

        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }

    function request_pendaftaran_after_insert($post_array, $primary_key) {
        $this->load->model("model_transaksi");
        if ($post_array["id_member"]) {
            $this->model_transaksi->update_alias_upline($primary_key, $post_array["id_member"]);
        }
    }

    function request_pendaftaran_after_update($post_array, $primary_key) {
        $this->load->model("model_transaksi");
        $is_double = $this->model_transaksi->cek_double_update($primary_key);
        if ($is_double == 0) {
            $id_req_pin = 0;
            if ($post_array["is_approved"] === "1") {
                $pin_asli = rand(100000, 999999);
                $pin = strtoupper(md5($pin_asli));
                $alias_member = $this->model_transaksi->add_new_member($post_array["no_hp_registran"], $post_array["nama_registran"], $post_array["id_member"], $post_array["email_registran"], $pin, $pin_asli);
                $this->model_transaksi->insert_transaksi(array(
                    1, "M70001", 'AKTIF', "ID : " . $alias_member, $post_array["nama_registran"], $post_array["no_hp_registran"], '00'
                ));
                $id_req_pin = -7;
            }
            $this->model_transaksi->update_data_pendaftaran($primary_key, $id_req_pin, $post_array["is_approved"]);
        }
    }

    function request_pin() {
        $crud = new ExCRUD();
        $crud->standartCheck();
        $crud->add_action('Edit Request', base_url('static/images/edit.png'), 'transaksi/request_pin_edit/edit');
        $crud->set_table('v_request_pin');
        $crud->set_primary_key("id_request");
        $crud->where("status", 0);

        $list_column_views = array('status', 'id_request');
        $crud->unset_columns($list_column_views);

        $crud->order_by('id_request', 'asc');
        $crud->set_add(false);
        $crud->set_edit(false);
        $crud->set_delete(false);
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }

    function request_pin_edit() {
        $crud = new ExCRUD();
        if ($crud->getState() != "edit" && $crud->getState() != "update_validation" && $crud->getState() != "update") {
            header("location:" . base_url("index.php/transaksi/request_pin"));
        }
        $crud->standartCheck();

        // 1. set table & relation
        $crud->set_table('request_pin');
        $crud->set_relation('id_member', 'member', '{alias_member} ({nickname})');
        $crud->set_primary_key("id_request");
        $crud->display_as("alias_member", "ID Member");

        // 4. aturan kolom                
        $list_column_hidden = array('id_member', 'id_request', 'key_token', 'rate_beli', 'rate_jual');
        $list_column_ro = array('rate_jual', 'alias_member', 'tanggal_request', 'id_members');
        $crud->unset_fields(array("key_token", "eksekutor", "tanggal_eksekusi"));
        $crud->field_type('status', 'dropdown', array('1' => 'TERIMA PERMINTAAN', '-1' => 'TOLAK PERMINTAAN'));
        $required_fields = array('status');
        $crud->set_field_readonly($crud, $list_column_ro);
        $crud->set_field_hidden($crud, $list_column_hidden);
        $crud->required_fields($required_fields);

        $crud->callback_before_update(array($this, 'request_pin_before_update'));
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }

    function request_pin_before_update($post_array, $primary_key) {
        $this->load->model("model_request");
        $is_double = $this->model_request->cek_double_update($primary_key, "request_pin");
        if ($is_double == 0) {
            if ($post_array["status"] === "1") {
                $token = substr(strtoupper(md5(rand(0, 100) . microtime(true))), 0, 20);
                $this->model_request->update_token($primary_key, $token);
                $data_member = $this->model_request->get_data_member($primary_key, "request_pin");
                $this->model_request->insert_transaksi(array(
                    $data_member->id_member, $data_member->alias_member, 'PIN', $primary_key, "", "", '00', $post_array["jumlah_beli"], $post_array['rate_beli'], $post_array['rate_jual']
                ));
            }
        }
    }

    function request_poin() {
        $crud = new ExCRUD();
        $crud->standartCheck();
        $crud->add_action('Edit Request', base_url('static/images/edit.png'), 'transaksi/request_poin_edit/edit');
        $crud->set_table('v_request_poin');
        $crud->set_primary_key("id_request");
        $crud->where("status", 0);

        $list_column_views = array('status', 'id_request');
        $crud->unset_columns($list_column_views);

        $crud->order_by('id_request', 'asc');
        $crud->set_add(false);
        $crud->set_edit(false);
        $crud->set_delete(false);
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }

    function request_poin_edit() {
        $crud = new ExCRUD();
        if ($crud->getState() != "edit" && $crud->getState() != "update_validation" && $crud->getState() != "update") {
            header("location:" . base_url("index.php/transaksi/request_poin"));
        }
        $crud->standartCheck();

        // 1. set table & relation
        $crud->set_table('request_poin');
        $crud->set_relation('id_member', 'member', '{alias_member} ({nickname})');
        $crud->set_primary_key("id_request");
        $crud->display_as("alias_member", "ID Member");

        // 4. aturan kolom                
        $list_column_hidden = array('id_member', 'id_request', 'rate_beli', 'rate_jual');
        $list_column_ro = array('rate_harga', 'alias_member', 'tanggal_request', 'id_members');
        $crud->unset_fields(array("eksekutor", "tanggal_eksekusi"));
        $crud->field_type('status', 'dropdown', array('1' => 'TERIMA PERMINTAAN', '-1' => 'TOLAK PERMINTAAN'));
        $required_fields = array('status');
        $crud->set_field_readonly($crud, $list_column_ro);
        $crud->set_field_hidden($crud, $list_column_hidden);
        $crud->required_fields($required_fields);

        $crud->callback_before_update(array($this, 'request_poin_before_update'));
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }

    function request_poin_before_update($post_array, $primary_key) {
        $this->load->model("model_request");
        $this->load->model("model_transaksi");
        $is_double = $this->model_request->cek_double_update($primary_key, "request_poin");
        if ($is_double == 0) {
            if ($post_array["status"] === "1") {
                $data_member = $this->model_request->get_data_member($primary_key, "request_poin");
                $id_trx = $this->model_request->insert_transaksi(array(
                    $data_member->id_member, $data_member->alias_member, 'POIN', $primary_key, "", "", '00', $post_array["jumlah_beli"], $post_array['rate_beli'], $post_array['rate_jual']
                ));
                $this->model_request->approve_poin($primary_key, $id_trx);
                $this->model_transaksi->add_poin($data_member->id_member, $id_trx, "Pembelian PIN", intval($post_array["jumlah_beli"]));
            }
        }
    }

    function request_dinar() {
        $this->load->model("model_request");
        $crud = new ExCRUD();
        $crud->set_table('user');
        $data = array();
        $data["style"] = $crud->render();
        $data["list_data_service"] = $this->model_request->get_data_request_dinar();       
        $this->load->view('template/dinar', $data);
    }
    
    function request_pesawat() {
        $this->load->model("model_request");
        $crud = new ExCRUD();
        $crud->set_table('user');
        $data = array();
        $data["style"] = $crud->render();
        $data["list_data_service"] = $this->model_request->get_data_request_pesawat();       
        $this->load->view('template/pesawat', $data);
    }
    
    function request_hotel() {
        $this->load->model("model_request");
        $crud = new ExCRUD();
        $crud->set_table('user');
        $data = array();
        $data["style"] = $crud->render();
        $data["list_data_service"] = $this->model_request->get_data_request_hotel();       
        $this->load->view('template/hotel', $data);
    }
    
    function request_seminar() {
        $this->load->model("model_request");
        $crud = new ExCRUD();
        $crud->set_table('user');
        $data = array();
        $data["style"] = $crud->render();
        $data["list_data_service"] = $this->model_request->get_data_request_seminar();       
        $this->load->view('template/seminar', $data);
    }
    
    function redeem_tour($is_special_tour = 0) {
        $this->load->model("model_request");
        $crud = new ExCRUD();
        $crud->set_table('user');
        $data = array();
        $data["style"] = $crud->render();
        $data["is_special_tour"] = $is_special_tour;
        $data["list_data_service"] = $this->model_request->get_data_redeem_tour($is_special_tour);
        $this->load->view('template/request_tour', $data);
    }
    
    function request_dinar_edit() {
        $crud = new ExCRUD();
        if ($crud->getState() != "edit" && $crud->getState() != "update_validation" && $crud->getState() != "update") {
            header("location:" . base_url("index.php/transaksi/request_dinar"));
        }
        $crud->standartCheck();

        // 1. set table & relation
        $crud->set_table('request_dinar');
        $crud->set_relation('id_member', 'member', '{alias_member} ({nickname})');
        $crud->set_primary_key("id_request");
        $crud->display_as("alias_member", "ID Member");

        // 4. aturan kolom                
        $list_column_hidden = array('id_member');
        $list_column_ro = array('harga_beli','harga_jual', 'alias_member', 'request_time', 'id_members');        
        
        global $g_list_status_dinar;
        $crud->field_type('status', 'dropdown', $g_list_status_dinar);
        
        $required_fields = array('status');
        $crud->set_field_readonly($crud, $list_column_ro);
        $crud->set_field_hidden($crud, $list_column_hidden);
        $crud->required_fields($required_fields);
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }
    
    function request_pesawat_edit() {
        $crud = new ExCRUD();
        if ($crud->getState() != "edit" && $crud->getState() != "update_validation" && $crud->getState() != "update") {
            header("location:" . base_url("index.php/transaksi/request_pesawat"));
        }
        $crud->standartCheck();

        // 1. set table & relation
        $crud->set_table('request_pesawat');
        $crud->set_relation('id_member', 'member', '{alias_member} ({nickname})');
        $crud->set_primary_key("id_request");
        $crud->display_as("alias_member", "ID Member");

        // 4. aturan kolom                
        $list_column_hidden = array('id_member');
        $list_column_ro = array('alias_member', 'date_created', 'id_members');        
        
        global $g_list_status_pesawat;
        $crud->field_type('status', 'dropdown', $g_list_status_pesawat);
        
        $required_fields = array('status');
        $crud->set_field_readonly($crud, $list_column_ro);
        $crud->set_field_hidden($crud, $list_column_hidden);
        $crud->required_fields($required_fields);
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }
    
    function request_hotel_edit() {
        $crud = new ExCRUD();
        if ($crud->getState() != "edit" && $crud->getState() != "update_validation" && $crud->getState() != "update") {
            header("location:" . base_url("index.php/transaksi/request_hotel"));
        }
        $crud->standartCheck();

        // 1. set table & relation
        $crud->set_table('request_hotel');
        $crud->set_relation('id_member', 'member', '{alias_member} ({nickname})');
        $crud->set_primary_key("id_request");
        $crud->display_as("alias_member", "ID Member");

        // 4. aturan kolom                
        $list_column_hidden = array('id_member');
        $list_column_ro = array('alias_member', 'date_created', 'id_members');        
        
        global $g_list_status_hotel;
        $crud->field_type('status', 'dropdown', $g_list_status_hotel);
        
        $required_fields = array('status');
        $crud->set_field_readonly($crud, $list_column_ro);
        $crud->set_field_hidden($crud, $list_column_hidden);
        $crud->required_fields($required_fields);
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }
    
    function request_seminar_edit() {
        $crud = new ExCRUD();
        if ($crud->getState() != "edit" && $crud->getState() != "update_validation" && $crud->getState() != "update") {
            header("location:" . base_url("index.php/transaksi/request_seminar"));
        }
        $crud->standartCheck();

        // 1. set table & relation
        $crud->set_table('request_seminar');
        $crud->set_relation('id_seminar', 'data_seminar', '{judul}');
        $crud->set_primary_key("id_request");        

        // 4. aturan kolom                
        $list_column_ro = array('request_date','harga_jual', 'alias_member', 'request_time', 'id_members');        
        
        global $g_list_status_seminar;
        $crud->field_type('is_registered', 'dropdown', $g_list_status_seminar);
        $crud->display_as("is_registered","status");
        
        $required_fields = array('status');
        $crud->set_field_readonly($crud, $list_column_ro);
        $crud->required_fields($required_fields);
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }
    
    function redeem_tour_edit() {
        $crud = new ExCRUD();
        if ($crud->getState() != "edit" && $crud->getState() != "update_validation" && $crud->getState() != "update") {
            header("location:" . base_url("index.php/transaksi/redeem_tour"));
        }
        $crud->standartCheck();

        // 1. set table & relation
        $crud->set_table('request_tour');
        $crud->set_relation('id_member', 'member', '{alias_member} ({nickname})');        
        $crud->set_primary_key("id_request");
        $crud->display_as("alias_member", "ID Member");

        // 4. aturan kolom                
        $list_column_hidden = array('id_member','id_tour','id_tour_schedule','date_approval','approved_by','progress_report');
        $list_column_ro = array('poin_value','date_request','harga_rupiah');        
        
        global $g_list_status_redeem;
        $crud->field_type('is_processed', 'dropdown', $g_list_status_redeem);
        
        $required_fields = array('is_processed');
        $crud->set_field_readonly($crud, $list_column_ro);
        $crud->set_field_hidden($crud, $list_column_hidden);
        $crud->required_fields($required_fields);
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }

}

/* End of file main.php */
/* Location: ./application/controllers/main.php */
